package com.yida.wy.config.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.file.MimeTypeUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.Logical;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.system.api.RemoteFileService;
import com.yida.wy.common.domain.WyUploadFile;
import com.yida.wy.config.domain.WyPolicyNotice;
import com.yida.wy.config.domain.dto.WyPolicyNoticeDetailDTO;
import com.yida.wy.config.domain.dto.WyPolicyNoticePageDTO;
import com.yida.wy.config.domain.request.WyPolicyNoticeChangeStatusRequest;
import com.yida.wy.config.domain.request.WyPolicyNoticeEditRequest;
import com.yida.wy.config.domain.request.WyPolicyNoticePageRequest;
import com.yida.wy.config.domain.request.WyPolicyNoticeSaveRequest;
import com.yida.wy.config.service.IWyPolicyNoticeService;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 公维金政策公告（小区信息公示）Controller
 *
 * @author ruoyi
 * @date 2023-05-30
 */
@RestController
@RequestMapping("/policy/notice")
@Api(tags = "公维金政策公告管理")
public class GwjPolicyNoticeController extends BaseController {
    @Autowired
    private IWyPolicyNoticeService gwjPolicyNoticeService;

    @Autowired
    private RemoteFileService remoteFileService;

    /**
     * 查询公维金政策公告列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询公维金政策公告列表", response = WyPolicyNoticePageDTO.class)
    public TableDataInfo list(WyPolicyNoticePageRequest pageRequest) {
        startPage();
        List<WyPolicyNotice> list = gwjPolicyNoticeService.selectGwjPolicyNoticeList(BeanUtil.copyProperties(pageRequest, WyPolicyNotice.class));
        return getDataDtoTable(list, WyPolicyNoticePageDTO.class);
    }

    /**
     * 获取公维金政策公告详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    @ApiOperation(value = "获取公维金政策公告详细信息", response = WyPolicyNoticeDetailDTO.class)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(gwjPolicyNoticeService.getDetail(id));
    }

    /**
     * 新增公维金政策公告
     */
    @RequiresPermissions(value = {"gwj:config:notice:add", "gwj:config:community:notice:add"}, logical = Logical.OR)
    @Log(title = "公维金政策公告", businessType = BusinessType.INSERT, systemName = "gwj")
    @PostMapping
    @ApiOperation("新增公维金政策公告")
    public AjaxResult add(@Validated @RequestBody WyPolicyNoticeSaveRequest saveRequest) {
        return toAjax(gwjPolicyNoticeService.saveGwjPolicyNotice(saveRequest));
    }

    /**
     * 修改公维金政策公告
     */
    @RequiresPermissions(value = {"gwj:config:notice:edit", "gwj:config:community:notice:edit"}, logical = Logical.OR)
    @Log(title = "公维金政策公告", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PutMapping
    @ApiOperation(("修改公维金政策公告"))
    public AjaxResult edit(@Validated @RequestBody WyPolicyNoticeEditRequest editRequest) {
        return toAjax(gwjPolicyNoticeService.editGwjPolicyNotice(editRequest));
    }

    /**
     * 删除公维金政策公告
     */
    @RequiresPermissions(value = {"gwj:config:notice:remove", "gwj:config:community:notice:remove"}, logical = Logical.OR)
    @Log(title = "公维金政策公告", businessType = BusinessType.DELETE, systemName = "gwj")
    @DeleteMapping("/{ids}")
    @ApiOperation("删除公维金政策公告")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gwjPolicyNoticeService.deleteGwjPolicyNoticeByIds(ids));
    }

    /**
     * 修改发布状态
     */
    @RequiresPermissions(value = {"gwj:config:notice:change", "gwj:config:community:notice:change"}, logical = Logical.OR)
    @Log(title = "修改公维金政策公告发布状态", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PostMapping("/changeStatus")
    @ApiOperation("修改发布状态")
    public AjaxResult changeStatus(@Validated @RequestBody WyPolicyNoticeChangeStatusRequest changeStatusRequest) {
        return toAjax(gwjPolicyNoticeService.changeStatus(changeStatusRequest));
    }

    /**
     * 公维金政策公告文件上传
     *
     * @param file
     * @return
     */
    @Log(title = "公维金政策公告文件上传", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "公维金政策公告文件上传接口", response = WyUploadFile.class)
    public R uploadFile(@RequestPart("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.fail("未获取到上传图片信息！请确认后提交！");
        }
        String extension = FileTypeUtils.getExtension(file);
        if (!StringUtils.equalsAnyIgnoreCase(extension, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION)) {
            return R.fail("文件格式不正确，请上传" + Arrays.toString(MimeTypeUtils.IMAGE_EXTENSION) + "格式");
        }
        return remoteFileService.upload(file);
    }
}
